<?php

/**
 * This file holds the library functions for player management.
 * 
 * @package Game_library
 */


require_once("lib/db.lib.php");
require_once("lib/table.lib.php");
require_once("conf/game.conf.php");

/**
 * Play a card in the player's hand.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round of game.
 * @param	int		$turn			Turn of the round.
 * @param	int		$card			Card to be played.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function player_play_card($game_id, $user_id, $round, $turn, $card){
	
	if(game_get_status($game_id) != GAME_STATUS_PLAYING){
		return FALSE;
	}
	
	db_connect();
	
	if(!player_is_card_valid($game_id, $user_id, $round, $turn, $card)){
		return FALSE;
	}
	
	$sql = "UPDATE `player_dealt_hand` SET `played_turn` = \"$turn\" WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\" AND `card` = \"$card\" ;";
	
	$result = mysql_query($sql);	
	
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;
	
	
}

/**
 * Check if a card can be played in that turn.
 *
 * i.e check if the card have been played, and if that turn has played cards.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round of game.
 * @param	int		$turn			Turn of the round.
 * @param	int		$card			Card to be played.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function player_is_card_valid($game_id, $user_id, $round, $turn, $card){
	db_connect();
	
	
	//check if card is played
	$card_ok = FALSE;
	$sql = "SELECT `played_turn` FROM `player_dealt_hand` WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\" AND `card` = \"$card\";";
	
	$result = mysql_query($sql);
	if(mysql_num_rows($result) > 0){
		$row = mysql_fetch_assoc($result);
		if($row['played_turn'] == NULL){
			$card_ok = TRUE;
		}
	}
	
	//check if turn has played cards.
	if($card_ok){
		$sql = "SELECT `played_turn` FROM `player_dealt_hand` WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\" AND `played_turn` = \"$turn\";";
		
		$result = mysql_query($sql);
		
		if(mysql_num_rows($result) == 0){
			return TRUE;
		}
		
	}
	
	return FALSE;
}

/**
 * Play a card in the player's hand. Will auto set the current round and turn of the game.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$card			Card to be played.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function player_play_card_auto($game_id, $user_id, $card){
	
	$round = game_get_current_round($game_id);
	$turn = game_get_current_turn($game_id);
	
	return player_play_card($game_id, $user_id, $round, $turn, $card);
}


/**
 * Set the waterlevel of a player in each turn
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round of game.
 * @param	int		$waterlevel		Waterlevel to be setted.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function player_set_waterlevel($game_id, $user_id, $round, $waterlevel){

	db_connect();
	
	$sql = "UPDATE `player_recv_waterlevel` SET `waterlevel` = \"$waterlevel\" WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\";";
	
	$result = mysql_query($sql);
	
	//return
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;	
	
}

/**
 * Set the turn of all players in a round
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$round			Round of game.
 * @param	int		$turn			Turn of the round.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function player_set_turn($game_id, $round, $turn){

	db_connect();
	
	$sql = "UPDATE `player_recv_waterlevel` SET `turn` = \"$turn\"  WHERE `game_id` = \"$game_id\" AND `round` = \"$round\";";
	
	$result = mysql_query($sql);
	
	//return
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;	
	
}



/**
 * Set the buoy remaining of a player in each turn
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round of game.
 * @param	int		$buoy_remain	Buoy remaining in the current turn.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function player_set_buoy_remain($game_id, $user_id, $round, $buoy_remain){
	
	db_connect();
	
	$sql = "UPDATE `player_recv_waterlevel` SET `buoy_remain` = \"$buoy_remain\" WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\";";
	
	$result = mysql_query($sql);
	
	//return
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;	
}


/**
 * Get the current waterlevel of a user.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round of game.
 * 
 * @return	int|FALSE	<b>Current waterlevel</b> of user, <b>FALSE</b> if failed.
 */
function player_get_current_waterlevel($game_id, $user_id, $round){

	db_connect();
	
	$sql = "SELECT `waterlevel` FROM `player_recv_waterlevel`WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\";";
	
	$result = mysql_query($sql);
	
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['waterlevel'];
	}
		
	return FALSE;
}


/**
 * Get the number of remaining buoys of a user.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round of game.
 * 
 * @return	int|FALSE	<b>Number of remaining buoys</b> of user, <b>FALSE</b> if failed.
 */
function player_get_buoy_remain($game_id, $user_id, $round){

	db_connect();
	
	$sql = "SELECT `buoy_remain` FROM `player_recv_waterlevel`WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\";";
	
	$result = mysql_query($sql);
	
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['buoy_remain'];
	}
		
	return FALSE;
}

/**
 * Get the hand of a user in a round.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round of game.
 * 
 * @return	Array|FALSE		<b>Array containing hand cards</b> of round, <b>FALSE</b> if failed.
 */
function player_get_hand($game_id, $user_id, $round){
	
	db_connect();
	
	$sql = "SELECT `card` FROM `player_dealt_hand` WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\" ORDER BY `card`;";
	
	$result = mysql_query($sql);

	if(mysql_num_rows($result) > 0){
		$card_arr = array();
		while($row = mysql_fetch_assoc($result)){
			array_push($card_arr, $row['card']);
			
		}
	
		return $card_arr;
	}
	
		
	return FALSE;
}

/**
 * Get the remaining hand of a user in a round.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round of game.
 * 
 * @return	Array|FALSE		<b>Array containing hand cards</b> of round, <b>FALSE</b> if failed.
 */
function player_get_hand_remain($game_id, $user_id, $round){
	
	db_connect();
	
	$sql = "SELECT `card` FROM `player_dealt_hand` WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\" AND (isnull(`played_turn`)) ORDER BY `card`;";
	
	$result = mysql_query($sql);

	if(mysql_num_rows($result) > 0){
		$card_arr = array();
		while($row = mysql_fetch_assoc($result)){
			array_push($card_arr, $row['card']);
			
		}
	
		return $card_arr;
	}
	
		
	return FALSE;
}

/**
 * Get the played cards of a user in a round.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round of game.
 * 
 * @return	Array|FALSE		<b>Array containing hand cards</b> of round, <b>FALSE</b> if failed.
 */
function player_get_hand_played($game_id, $user_id, $round){
	
	db_connect();
	
	$sql = "SELECT `card` FROM `player_dealt_hand` WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\" AND (!isnull(`played_turn`)) ORDER BY `card`;";
	
	$result = mysql_query($sql);

	if(mysql_num_rows($result) > 0){
		$card_arr = array();
		while($row = mysql_fetch_assoc($result)){
			array_push($card_arr, $row['card']);
			
		}
	
		return $card_arr;
	}
	
		
	return FALSE;
}


/**
 * Get card played by a player
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round of game.
 * @param	int		$turn			Turn of round.
 * 
 * @return	int|FALSE	<b>Card played</b> by user, <b>FALSE</b> if failed.
 */
function player_get_played_card($game_id, $user_id, $round, $turn){

	db_connect();
	
	$sql = "SELECT `card` FROM `player_dealt_hand`WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$round\" AND `played_turn` = \"$turn\";";
	
	$result = mysql_query($sql);
	
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['card'];
	}
		
	return FALSE;
}

/**
 * Get card played by a player. Auto select the current round and turn.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * 
 * @return	int|FALSE	<b>Card played</b> by user, <b>FALSE</b> if failed.
 */
function player_get_played_card_auto($game_id, $user_id){

	db_connect();
	
	$round = game_get_current_round($game_id);
	$turn = game_get_current_turn($game_id);
	
	return player_get_played_card($game_id, $user_id, $round, $turn);
	
}


/**
 * Get the points of a player in a game
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * 
 * @return	int|FALSE	<b>Game points</b> of user, <b>FALSE</b> if failed.
 */
function player_get_current_points($game_id, $user_id){

	db_connect();
	
	$sql = "SELECT `game_points` FROM `game_has_user` WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\";";
	
	$result = mysql_query($sql);
	
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['game_points'];
	}
		
	return FALSE;
}



